package org.jeecg.modules.system.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.system.entity.*;
import org.jeecg.modules.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.Optional;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @Author scott
 * @since 2018-12-20
 */
@Slf4j
@RestController
@RequestMapping("/sys/product")
public class ProductController {

    @Autowired
    private ISysAirProductService airProductService;

    @Autowired
    private ISysLandProductService landProductService;

    @Autowired
    private ISysLessProductService lessProductService;

    @Autowired
    private ISysProductOtherSettingService otherSettingService;

    @GetMapping("/list/{type}")
    public Result list(@PathVariable String type, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "1000") Integer pageSize) {
        if (type.equals("air")) {
            Page<SysAirProduct> page = new Page<SysAirProduct>(pageNo, pageSize);
            Page<SysAirProduct> page1 = airProductService.page(page);
            Optional<SysAirProduct> first = page1.getRecords().stream().sorted(Comparator.comparing(SysAirProduct::getUpdateTime).reversed()).findFirst();
            Date updateTime =DateUtil.date();
            if(first.isPresent()){
                updateTime = first.get().getUpdateTime();
            }
            return Result.OK(page1).success(DateUtil.formatDateTime(updateTime));
        } else if (type.equals("land")) {
            Page<SysLandProduct> page = new Page<SysLandProduct>(pageNo, pageSize);
            Page<SysLandProduct> page1 = landProductService.page(page);
            Optional<SysLandProduct> first = page1.getRecords().stream().sorted(Comparator.comparing(SysLandProduct::getUpdateTime).reversed()).findFirst();
            Date updateTime =DateUtil.date();
            if(first.isPresent()){
                updateTime = first.get().getUpdateTime();
            }
            return Result.OK(page1).success(DateUtil.formatDateTime(updateTime));
        } else if (type.equals("less")) {
            Page<SysLessProduct> page = new Page<SysLessProduct>(pageNo, pageSize);
            Page<SysLessProduct> page1 = lessProductService.page(page,Wrappers.<SysLessProduct>lambdaQuery().orderByAsc(SysLessProduct::getSortNum));
            Optional<SysLessProduct> first = page1.getRecords().stream().sorted(Comparator.comparing(SysLessProduct::getUpdateTime).reversed()).findFirst();
            Date updateTime =DateUtil.date();
            if(first.isPresent()){
                updateTime = first.get().getUpdateTime();
            }
            return Result.OK(page1).success(DateUtil.formatDateTime(updateTime));
        } else {
            return Result.OK();
        }
    }


    @PostMapping("/edit/{type}")
    public Result edit(@PathVariable String type, @RequestBody JSONObject data) {
        if (type.equals("air")) {
            SysAirProduct sysAirProduct = JSONObject.parseObject(String.valueOf(data), SysAirProduct.class);
            if (StringUtils.isBlank(sysAirProduct.getResetFilter())) {
                return Result.error("重量条件不能为空");
            }

            try {
                String[] split = sysAirProduct.getResetFilter().split("-");
                if (split.length == 1) {
                    if (sysAirProduct.getResetFilter().contains(">")) {
                        String kg = sysAirProduct.getResetFilter().replaceAll(">", "").replaceAll("KG", "");
                        sysAirProduct.setMowMax(new BigDecimal(kg.trim()));
                        sysAirProduct.setMowMin(null);
                    } else if (sysAirProduct.getResetFilter().contains("<")) {
                        String kg = sysAirProduct.getResetFilter().replaceAll("<", "").replaceAll("KG", "");
                        sysAirProduct.setMowMin(new BigDecimal(kg.trim()));
                        sysAirProduct.setMowMax(null);
                    } else {
                        return Result.error("重量条件表达式错误");
                    }
                } else if (split.length == 2) {
                    String s = split[0].replaceAll("KG", "");
                    String e = split[1].replaceAll("KG", "");
                    sysAirProduct.setMowMin(new BigDecimal(s.trim()));
                    sysAirProduct.setMowMax(new BigDecimal(e.trim()));
                } else {
                    return Result.error("重量条件表达式错误");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error("重量条件表达式有误");
            }


            sysAirProduct.setMow("莫斯科");
            airProductService.updateById(sysAirProduct);
            return Result.OK();
        } else if (type.equals("land")) {
            SysLandProduct sysLandProduct = JSONObject.parseObject(String.valueOf(data), SysLandProduct.class);

            if (StringUtils.isBlank(sysLandProduct.getResetFilter())) {
                return Result.error("重量条件不能为空");
            }

            try {
                String[] split = sysLandProduct.getResetFilter().split("-");
                if (split.length == 1) {
                    if (sysLandProduct.getResetFilter().contains(">")) {
                        String kg = sysLandProduct.getResetFilter().replaceAll(">", "").replaceAll("KG", "");
                        sysLandProduct.setMowMax(new BigDecimal(kg.trim()));
                        sysLandProduct.setMowMin(null);
                    } else if (sysLandProduct.getResetFilter().contains("<")) {
                        String kg = sysLandProduct.getResetFilter().replaceAll("<", "").replaceAll("KG", "");
                        sysLandProduct.setMowMin(new BigDecimal(kg.trim()));
                        sysLandProduct.setMowMax(null);
                    } else {
                        return Result.error("重量条件表达式错误");
                    }
                } else if (split.length == 2) {
                    String s = split[0].replaceAll("KG", "");
                    String e = split[1].replaceAll("KG", "");
                    sysLandProduct.setMowMin(new BigDecimal(s.trim()));
                    sysLandProduct.setMowMax(new BigDecimal(e.trim()));
                } else {
                    return Result.error("重量条件表达式错误");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error("重量条件表达式有误");
            }

            sysLandProduct.setMow("莫斯科");
            sysLandProduct.setMowOut("莫斯科以外");
            landProductService.updateById(sysLandProduct);
            return Result.OK();
        } else if (type.equals("less")) {
            SysLessProduct sysLessProduct = JSONObject.parseObject(String.valueOf(data), SysLessProduct.class);
            if (StringUtils.isBlank(sysLessProduct.getResetFilter())) {
                return Result.error("运价系数不能为空");
            }

            try {
                String[] split = sysLessProduct.getResetFilter().split("-");
                if (split.length == 1) {
                    if (sysLessProduct.getResetFilter().contains(">")) {
                        String kg = sysLessProduct.getResetFilter().replaceAll(">", "");
                        sysLessProduct.setMowMax(new BigDecimal(kg.trim()));
                        sysLessProduct.setMowMin(null);
                    } else if (sysLessProduct.getResetFilter().contains("<")) {
                        String kg = sysLessProduct.getResetFilter().replaceAll("<", "");
                        sysLessProduct.setMowMin(new BigDecimal(kg.trim()));
                        sysLessProduct.setMowMax(null);
                    } else {
                        return Result.error("运价系数表达式错误");
                    }
                } else if (split.length == 2) {
                    String s = split[0];
                    String e = split[1];
                    sysLessProduct.setMowMin(new BigDecimal(s.trim()));
                    sysLessProduct.setMowMax(new BigDecimal(e.trim()));
                } else {
                    return Result.error("运价系数表达式错误");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error("运价系数表达式有误");
            }

            sysLessProduct.setMow("莫斯科");
            sysLessProduct.setMowOut("莫斯科以外");
            lessProductService.updateById(sysLessProduct);
            return Result.OK();
        } else {
            return Result.error("无效的产品类型");
        }
    }

    @GetMapping("/otherInfo/{type}")
    public Result getOtherInfo(@PathVariable String type) {
        SysProductOtherSetting one = otherSettingService.getOne(Wrappers.<SysProductOtherSetting>lambdaQuery()
                .eq(SysProductOtherSetting::getType, type)
        );
        return Result.OK(one);
    }


    @PostMapping("/edit/otherInfo")
    public Result editOtherInfo(@RequestBody SysProductOtherSetting productOtherSetting) {
        otherSettingService.updateById(productOtherSetting);
        return Result.OK();
    }

}
